package edu.hawaii.kukui

class KukuiUserPictureController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

      def image_r = {
    def i = KukuiUserPicture.get(params.id)
    byte[] imagepic = i.userPicture
    //response.contentType = "image/jpeg"
    //response.setContentLength(i.picture.length)
    //response.outputStream.write(userImage.picture)
    //response.setHeader( "Content-Type", "image/png")
    response.outputStream << imagepic
    //response.outputStream.flush()
  }

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [kukuiUserPictureInstanceList: KukuiUserPicture.list(params), kukuiUserPictureInstanceTotal: KukuiUserPicture.count()]
    }

    def create = {
        def kukuiUserPictureInstance = new KukuiUserPicture()
        kukuiUserPictureInstance.properties = params
        return [kukuiUserPictureInstance: kukuiUserPictureInstance]
    }

    def save = {
        def kukuiUserPictureInstance = new KukuiUserPicture(params)
        if (kukuiUserPictureInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'kukuiUserPicture.label', default: 'KukuiUserPicture'), kukuiUserPictureInstance.id])}"
            redirect(action: "show", id: kukuiUserPictureInstance.id)
        }
        else {
            render(view: "create", model: [kukuiUserPictureInstance: kukuiUserPictureInstance])
        }
    }

    def show = {
        def kukuiUserPictureInstance = KukuiUserPicture.get(params.id)
        if (!kukuiUserPictureInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'kukuiUserPicture.label', default: 'KukuiUserPicture'), params.id])}"
            redirect(action: "list")
        }
        else {
            [kukuiUserPictureInstance: kukuiUserPictureInstance]
        }
    }

    def edit = {
        def kukuiUserPictureInstance = KukuiUserPicture.get(params.id)
        if (!kukuiUserPictureInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'kukuiUserPicture.label', default: 'KukuiUserPicture'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [kukuiUserPictureInstance: kukuiUserPictureInstance]
        }
    }

    def update = {
        def kukuiUserPictureInstance = KukuiUserPicture.get(params.id)
        if (kukuiUserPictureInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (kukuiUserPictureInstance.version > version) {
                    
                    kukuiUserPictureInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'kukuiUserPicture.label', default: 'KukuiUserPicture')] as Object[], "Another user has updated this KukuiUserPicture while you were editing")
                    render(view: "edit", model: [kukuiUserPictureInstance: kukuiUserPictureInstance])
                    return
                }
            }
            kukuiUserPictureInstance.properties = params
            if (!kukuiUserPictureInstance.hasErrors() && kukuiUserPictureInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'kukuiUserPicture.label', default: 'KukuiUserPicture'), kukuiUserPictureInstance.id])}"
                redirect(action: "show", id: kukuiUserPictureInstance.id)
            }
            else {
                render(view: "edit", model: [kukuiUserPictureInstance: kukuiUserPictureInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'kukuiUserPicture.label', default: 'KukuiUserPicture'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def kukuiUserPictureInstance = KukuiUserPicture.get(params.id)
        if (kukuiUserPictureInstance) {
            try {
                kukuiUserPictureInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'kukuiUserPicture.label', default: 'KukuiUserPicture'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'kukuiUserPicture.label', default: 'KukuiUserPicture'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'kukuiUserPicture.label', default: 'KukuiUserPicture'), params.id])}"
            redirect(action: "list")
        }
    }
}
